package cn.edu.sziit.bbs.controller;

import cn.edu.sziit.bbs.model.Post;
import cn.edu.sziit.bbs.model.Reply;
import cn.edu.sziit.bbs.model.User;
import cn.edu.sziit.bbs.service.PostService;
import cn.edu.sziit.bbs.service.ReplyService;
import cn.edu.sziit.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class MyCenterController {

    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    @Autowired
    private ReplyService replyService;


    @RequestMapping(value = "/myCenter", method = RequestMethod.GET)
    public String myCenterShow(HttpSession session) {

        User user = (User) session.getAttribute("user");

        List<Post> userPosts = postService.selectAllByUserId(user.getUserId());
        List<Reply> userReplies = replyService.selectAllByUserId(user.getUserId());

        System.out.println(userPosts);
        session.setAttribute("userPosts", userPosts);
        session.setAttribute("userReplies", userReplies);

        return "myCenter";
    }

    @ResponseBody
    @RequestMapping(value = "/userUpdate", method = RequestMethod.POST)
    public Map<String, String> userUpdate(User user, String userOldPassword, HttpSession session) {
        System.out.println(user);
        System.out.println(userOldPassword);

        Map<String, String> map = new HashMap<>();

        User oldUser = (User) session.getAttribute("user");

        if (!oldUser.getUserLevel().equals("admin")) {
            if (!oldUser.getUserPassword().equals(userOldPassword) && !user.getUserPassword().equals("")) {
                map.put("info", "原密码错误");
                return map;
            }
            if (user.getUserName().equals("")) {
                user.setUserName(oldUser.getUserName());
            }

            if (user.getUserPassword().equals("")) {
                user.setUserPassword(oldUser.getUserPassword());
            }

            user.setUserId(oldUser.getUserId());
        }


        String info = userService.updateByPrimaryKeySelective(user);

        map.put("info", info);

        if (oldUser.getUserLevel().equals("user")) {
            user = userService.selectByUsername(user.getUserName());
            session.setAttribute("user", user);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/postDelete", method = RequestMethod.GET)
    public String postDelete(Integer postId) {

        postService.deleteByPrimaryKey(postId);

        return "删除帖子成功";
    }

    @RequestMapping(value = "/userPhotoUpload", method = RequestMethod.POST)
    public String userPhotoUpload(HttpServletRequest request, MultipartFile file, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");

        if (!file.getOriginalFilename().equals("")) {
            System.out.println(file.getOriginalFilename());
            String path = request.getServletContext().getRealPath("/static/images/user");
            System.out.println("path:" + path);
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                System.out.println("目录创建成功");
                pathFile.mkdir();
            }
            System.out.println(pathFile.getCanonicalPath());
            String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
            File serverFile = new File(path, fileName);
            file.transferTo(serverFile);

            user.setUserPhoto("/static/images/user/" + fileName);
        }

        userService.updateByPrimaryKeySelective(user);
        session.setAttribute("user", user);
        session.setAttribute("page", "");
        return "myCenter";
    }

}
